package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.constant.CacheKey;
import cc.yugu.yhdw.common.domain.hdw.CabinetStrategy;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import com.whalin.MemCached.MemCachedClient;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 终端策略业务切面类
 *
 * @Date:Created in 2018/7/9 9:53
 * @Author:
 * @Modified By: zjn 2018/7/9 9:53
 */
@Component
@Aspect
public class CabinetStrategyServiceAspect {

    @Autowired
    MemCachedClient memCachedClient;

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.hdw.CabinetStrategyService.update(..))")
    private void update() {}
    /**
    * 更新
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/9  9:54
    */
    @Around("update()")
    public Object update(ProceedingJoinPoint joinPoint) throws Throwable {
        ExtResult result = (ExtResult)joinPoint.proceed();
        if(result.isSuccess()) {
            CabinetStrategy strategy = (CabinetStrategy) joinPoint.getArgs()[0];
            String key = CacheKey.key(CacheKey.K_ID_V_CABINET_STRATEGY_XML, strategy.getId());
            memCachedClient.delete(key);
        }
        return result;
    }

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.hdw.CabinetStrategyService.delete(..))")
    public void delete(){}
    /**
    * 删除
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/9  9:55
    */
    @Around("delete()")
    public Object delete(ProceedingJoinPoint joinPoint) throws Throwable {
        ExtResult result = (ExtResult)joinPoint.proceed();
        if(result.isSuccess()) {
            Integer id = (Integer) joinPoint.getArgs()[0];
            String key = CacheKey.key(CacheKey.K_ID_V_CABINET_STRATEGY_XML, id);
            memCachedClient.delete(key);
        }
        return result;
    }
}
